//
//  FaceLivenessViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/12/2.
//

// 判断是否是ipad
#define isPad ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)

// 判断iPhoneX
#define IS_IPHONE_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)
// 判断iPHoneXr
#define IS_IPHONE_Xr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)
// 判断iPhoneXs
#define IS_IPHONE_Xs ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)
// 判断iPhoneXs Max
#define IS_IPHONE_Xs_Max ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size) && !isPad : NO)

#define scaleValue 0.70
#define scaleValueX 0.80

#define ScreenRect [UIScreen mainScreen].bounds
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

/**iPhone6s为标准，乘以宽的比例*/
#define KScaleX(value) ((value)/375.0f * ScreenWidth)
/**iPhone6s为标准，乘以高的比例*/
#define KScaleY(value) ((value)/667.0f * ScreenHeight)

#import "FaceLivenessViewController.h"
#import "IDLFaceSDK/IDLFaceSDK.h"
#import "BDFaceVideoCaptureDevice.h"
#import "BDFaceCycleProgressView.h"
#import "BDFaceRemindAnimationView.h"
#import "BDFaceRemindView.h"
#import "FaceMonitorViewController.h"

@interface FaceLivenessViewController ()<CaptureDataOutputProtocol>
@property (nonatomic, readwrite, retain) BDFaceVideoCaptureDevice *videoCapture;
/**
 *  视频i流回显view
 */
@property (nonatomic, readwrite, retain) UIImageView *displayImageView;
/**
 * 人脸检测view，与视频流rect 一致
 */
@property (nonatomic, readwrite, assign) CGRect previewRect;
/**
 *  人脸预览view ，最大预览框之内，最小预览框之外，根据该view 提示离远离近
 */

@property (nonatomic, readwrite, assign) CGRect detectRect;

@property (nonatomic, readwrite, retain) UILabel *remindLabel;
@property (nonatomic, readwrite, retain) UILabel * remindDetailLabel;
/*
 *  动作活体动画
 */
@property (nonatomic,readwrite,retain) BDFaceRemindAnimationView *remindAnimationView;

/**
 *  进度条view，活体检测页面
 */
@property (nonatomic, readwrite, retain) BDFaceCycleProgressView *circleProgressView;

@property (nonatomic, readwrite, assign) BOOL hasFinished;
@property (nonatomic, readwrite, retain) BDFaceRemindView * remindView;

// 超时相关控件
@property (nonatomic, readwrite, retain) UIView *timeOutMainView;
@property (nonatomic, readwrite, retain) UIImageView *timeOutImageView;
@property (nonatomic, readwrite, retain) UIView *timeOutView;
@property (nonatomic, readwrite, retain) UILabel *timeOutLabel;
@property (nonatomic, readwrite, retain) UIView *timeOutLine;
@property (nonatomic, readwrite, retain) UIButton *timeOutRestartButton;
@property (nonatomic, readwrite, retain) UILabel *timeOutRestartLabel;
@property (nonatomic, readwrite, retain) UIView *timeOutLine2;
@property (nonatomic, readwrite, retain) UIButton *timeOutRestartButton2;
@property (nonatomic, readwrite, retain) UILabel *timeOutBackToMainLabel2;

@property(nonatomic,strong) NSArray *livenessArray;
@property (nonatomic, assign) BOOL isAnimating;
@end

@implementation FaceLivenessViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _hasFinished = NO;
    self.videoCapture.runningStatus = YES;
    [self.videoCapture startSession];
    [BDFaceVideoCaptureDevice initFaceSDK];
    [[IDLFaceLivenessManager sharedInstance] startInitial];
    [IDLFaceLivenessManager.sharedInstance reset];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [BDFaceVideoCaptureDevice destorySDK];
    self.hasFinished = YES;
    self.videoCapture.runningStatus = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    FaceLivenessActionTypeLiveEye = 0,
    //    FaceLivenessActionTypeLiveMouth = 1,
    //    FaceLivenessActionTypeLiveYawRight = 2,
    //    FaceLivenessActionTypeLiveYawLeft = 3,
    //    FaceLivenessActionTypeLivePitchUp = 4,
    //    FaceLivenessActionTypeLivePitchDown = 5,
    //    FaceLivenessActionTypeLiveYaw = 6,
    //    FaceLivenessActionTypeNoAction = 7,
    self.livenessArray = @[@(FaceLivenessActionTypeLiveEye),
                           @(FaceLivenessActionTypeLiveMouth),
                           @(FaceLivenessActionTypeLiveYawRight),
                            @(FaceLivenessActionTypeLiveYawLeft),
                           @(FaceLivenessActionTypeLivePitchUp),
                           @(FaceLivenessActionTypeLivePitchDown),
    ];
    
    [[IDLFaceLivenessManager sharedInstance] livenesswithList:self.livenessArray order:YES numberOfLiveness:self.livenessArray.count];
    
    // 初始化相机处理类
    self.videoCapture = [[BDFaceVideoCaptureDevice alloc] init];
    self.videoCapture.delegate = self;
    [IDLFaceLivenessManager sharedInstance].enableSound  = YES;
    // 图像采集声音
    [IDLFaceDetectionManager sharedInstance].enableSound = YES;
    
    if (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) {
        self.previewRect = CGRectMake(ScreenWidth*(1-scaleValueX)/2.0, ScreenHeight*(1-scaleValueX)/2.0, ScreenWidth*scaleValueX, ScreenHeight*scaleValueX);
    } else {
        self.previewRect = CGRectMake(ScreenWidth*(1-scaleValue)/2.0, ScreenHeight*(1-scaleValue)/2.0, ScreenWidth*scaleValue, ScreenHeight*scaleValue);
    }
    
    // 用于展示视频流的imageview
    self.displayImageView = [[UIImageView alloc] initWithFrame:self.previewRect];
    self.displayImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:self.displayImageView];
    
    CGRect circleRect =CGRectMake(ScreenWidth*(1-scaleValue)/2.0 , 175*ScreenHeight/667, ScreenWidth*scaleValue, ScreenWidth*scaleValue);
    
    // 画圈和圆形遮罩
    self.detectRect = CGRectMake(circleRect.origin.x , circleRect.origin.y, circleRect.size.width, circleRect.size.height*5/4);
    CGPoint centerPoint = CGPointMake(CGRectGetMidX(circleRect), CGRectGetMidY(circleRect));
    //创建一个View
    UIView *maskView = [[UIView alloc] initWithFrame:ScreenRect];
    maskView.backgroundColor = [UIColor whiteColor];
    maskView.alpha = 1;
    [self.view addSubview:maskView];
    //贝塞尔曲线 画一个带有圆角的矩形
    UIBezierPath *bpath = [UIBezierPath bezierPathWithRoundedRect:ScreenRect cornerRadius:0];
    //贝塞尔曲线 画一个圆形
    [bpath appendPath:[UIBezierPath bezierPathWithArcCenter:centerPoint radius:ScreenWidth*scaleValue / 2 startAngle:0 endAngle:2 * M_PI clockwise:NO]];
    //创建一个CAShapeLayer 图层
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = bpath.CGPath;
    // 添加图层蒙板
    maskView.layer.mask = shapeLayer;
    
    // 进度条view，活体检测页面
    CGRect circleProgressRect =  CGRectMake(CGRectGetMinX(circleRect) - 13.7, CGRectGetMinY(circleRect) - 13.7, CGRectGetWidth(circleRect) + (13.7 * 2), CGRectGetHeight(circleRect) + (13.7 * 2));
    self.circleProgressView = [[BDFaceCycleProgressView alloc] initWithFrame:circleProgressRect];
    // 刻度线背颜色
    self.circleProgressView.lineBgColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1 / 1.0];
    // 刻度线进度颜色
    self.circleProgressView.scaleColor =  [UIColor colorWithRed:0 / 255.0 green:186 / 255.0 blue:242 / 255.0 alpha:1 / 1.0];
    [self.view addSubview:self.circleProgressView];
    
    // 动作活体动画
    self.remindAnimationView = [[BDFaceRemindAnimationView alloc] initWithFrame:circleRect];
    [self.view addSubview:self.remindAnimationView];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.remindAnimationView setActionImages];
    });
    
    // 提示框（动作）
    self.remindLabel = [[UILabel alloc] init];
    self.remindLabel.frame = CGRectMake(0, 103.3, ScreenWidth, 22);
    self.remindLabel.textAlignment = NSTextAlignmentCenter;
    self.remindLabel.textColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:1 / 1.0];
    self.remindLabel.font = [UIFont boldSystemFontOfSize:22];
    [self.view addSubview:self.remindLabel];
    
    // 提示label（遮挡等问题）
    self.remindDetailLabel = [[UILabel alloc] init];
    self.remindDetailLabel.frame = CGRectMake(0, 139.3, ScreenWidth, 16);
    self.remindDetailLabel.font = [UIFont systemFontOfSize:16];
    self.remindDetailLabel.textColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1 / 1.0];
    self.remindDetailLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.remindDetailLabel];
    [self.remindDetailLabel setHidden:true];
    
    {
        // 超时的view初始化，但是不添加到当前view内
        // 超时的最底层view，大小和屏幕大小一致，为了突出弹窗的view的效果，背景为灰色，0.7的透视度
        _timeOutMainView = [[UIView alloc] init];
        _timeOutMainView.frame = ScreenRect;
        _timeOutMainView.alpha = 0.7;
        _timeOutMainView.backgroundColor = [UIColor grayColor];
    
        // 弹出的主体view
        self.timeOutView = [[UIView alloc] init];
        self.timeOutView.frame = CGRectMake(KScaleX(20), KScaleY(179.3), ScreenWidth-KScaleX(40), KScaleY(281.3));
        self.timeOutView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1 / 1.0];
        self.timeOutView.layer.cornerRadius = 7.5;
    
        // 超时的image
        _timeOutImageView = [[UIImageView alloc] init];
        _timeOutImageView.frame = CGRectMake((ScreenWidth-76) / 2, KScaleY(217.3), 76, 76);
        _timeOutImageView.image = [UIImage imageNamed:@"icon_overtime"];
    
        // 超时的label
        _timeOutLabel = [[UILabel alloc] init];
        _timeOutLabel.frame = CGRectMake(KScaleX(40), KScaleY(309.3), ScreenWidth-KScaleX(80), 22);
        _timeOutLabel.text = @"人脸采集超时";
        _timeOutLabel.textAlignment = NSTextAlignmentCenter;
        _timeOutLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18];
        _timeOutLabel.textColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:1 / 1.0];
    
        // 区分线
        _timeOutLine = [[UIView alloc] init];
        _timeOutLine.frame = CGRectMake((ScreenWidth-320) / 2, 361.2, 320, 0.3);
        _timeOutLine.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1 / 1.0];
    
    
        // 重新开始采集button
        _timeOutRestartButton = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-320)/2, KScaleY(376), 320, 18)];
        [_timeOutRestartButton addTarget:self action:@selector(reStart:) forControlEvents:UIControlEventTouchUpInside];
    
        // 重新采集的文字label
        _timeOutRestartLabel = [[UILabel alloc] init];
        _timeOutRestartLabel.frame = CGRectMake((ScreenWidth-72) / 2, KScaleY(376.3), 72, 18);
        _timeOutRestartLabel.text = @"重新采集";
        _timeOutRestartLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18];
        _timeOutRestartLabel.textColor = [UIColor colorWithRed:0 / 255.0 green:186 / 255.0 blue:242 / 255.0 alpha:1 / 1.0];
    
        // 区分线
        _timeOutLine2 = [[UIView alloc] init];
        _timeOutLine2.frame = CGRectMake((ScreenWidth-320) / 2, 409.2, 320, 0.3);
        _timeOutLine2.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1 / 1.0];
    
    
        // 回到首页的button
        _timeOutRestartButton2 = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-320)/2, KScaleY(424), 320, 18)];
        [_timeOutRestartButton2 addTarget:self action:@selector(backToPreView:) forControlEvents:UIControlEventTouchUpInside];
    
        // 回到首页的label
        _timeOutBackToMainLabel2 = [[UILabel alloc] init];
        _timeOutBackToMainLabel2.frame = CGRectMake((ScreenWidth-72) / 2, KScaleY(424.3), 72, 18);
        _timeOutBackToMainLabel2.text = @"回到首页";
        _timeOutBackToMainLabel2.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18];
        _timeOutBackToMainLabel2.textColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1 / 1.0];
    }
    
    // 监听重新返回APP
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillResignAction) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)back {
    _hasFinished = YES;
    self.videoCapture.runningStatus = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) setHasFinished:(BOOL)hasFinished {
    _hasFinished = hasFinished;
    if (hasFinished) {
        [self.videoCapture stopSession];
        self.videoCapture.delegate = nil;
    }
}
- (void)isTimeOut:(BOOL)isOrNot {
    if (isOrNot){
        // 加载超时的view
        [self outTimeViewLoad];
    }
}
- (void)outTimeViewLoad{
    
    // 显示超时view，并停止视频流工作
    self.remindLabel.text = @"";
    self.remindDetailLabel.text = @"";
    self.videoCapture.runningStatus = NO;
    [self.videoCapture stopSession];
    [self.view addSubview:_timeOutMainView];
    [self.view addSubview:_timeOutView];
    [self.view addSubview:_timeOutImageView];
    [self.view addSubview:_timeOutLabel];
    [self.view addSubview:_timeOutRestartButton];
    [self.view addSubview:_timeOutLine];
    [self.view addSubview:_timeOutRestartLabel];
    [self.view addSubview:_timeOutLine2];
    [self.view addSubview:_timeOutRestartButton2];
    [self.view addSubview:_timeOutBackToMainLabel2];
}

- (void)outTimeViewUnload{
    
    // 关闭超时的view，恢复视频流工作
    self.videoCapture.runningStatus = YES;
    [self.videoCapture startSession];
    [_timeOutMainView removeFromSuperview];
    [_timeOutView removeFromSuperview];
    [_timeOutImageView removeFromSuperview];
    [_timeOutLabel removeFromSuperview];
    [_timeOutRestartButton removeFromSuperview];
    [_timeOutLine removeFromSuperview];
    [_timeOutRestartLabel removeFromSuperview];
    [_timeOutLine2 removeFromSuperview];
    [_timeOutBackToMainLabel2 removeFromSuperview];
    [_timeOutRestartButton2 removeFromSuperview];
    
}
- (IBAction)reStart:(UIButton *)sender{
    // 对应页面去补充
    dispatch_async(dispatch_get_main_queue(), ^{
        [self outTimeViewUnload];
    });
    // 调用相应的部分设置
    [self selfReplayFunction];
    
}

- (void)selfReplayFunction{
    // 相应的功能在采集/检测时候写
    [[IDLFaceLivenessManager sharedInstance] reset];
    [[IDLFaceLivenessManager sharedInstance] livenesswithList:self.livenessArray order:YES numberOfLiveness:self.livenessArray.count];
}
- (void)backToPreView:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Notification

- (void)onAppWillResignAction {
    _hasFinished = YES;
    [IDLFaceLivenessManager.sharedInstance reset];
}

- (void)onAppBecomeActive {
    _hasFinished = NO;
    [[IDLFaceLivenessManager sharedInstance] livenesswithList:self.livenessArray order:YES numberOfLiveness:self.livenessArray.count];
}

#pragma mark - CaptureDataOutputProtocol

- (void)captureOutputSampleBuffer:(UIImage *)image {
    if (_hasFinished) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        weakSelf.displayImageView.image = image;
    });
    [self faceProcesss:image];
}
- (void)faceProcesss:(UIImage *)image {
    if (self.hasFinished) {
        return;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        self.isAnimating = [self.remindAnimationView isActionAnimating];
    });
    /*
     显示提示动画的过程中还可以做动作
     */
//    if (self.isAnimating){
//        return;
//    }
    
    __weak typeof(self) weakSelf = self;
    [[IDLFaceLivenessManager sharedInstance] livenessNormalWithImage:image previewRect:self.previewRect detectRect:self.detectRect completionHandler:^(NSDictionary *images, FaceInfo *faceInfo, LivenessRemindCode remindCode) {
//        NSLog(@"remindCode = %lu", (unsigned long)remindCode);
/*
 此注释里的代码用于显示人脸框，调试过程中需要显示人脸款可打开注释
 
 //        绘制人脸框功能，开发者可以通过观察人脸框_faceRectFit 在  previewRect 包含关系判断是框内还是框外
         dispatch_async(dispatch_get_main_queue(), ^{
             CGRect faceRect = [BDFaceQualityUtil getFaceRect:faceInfo.landMarks withCount:faceInfo.landMarks.count];
             CGRect faceRectFit = [BDFaceUtil convertRectFrom:faceRect image:image previewRect:previewRect];
             if (!isPaint) {
                 newImage= [[UIImageView alloc]init];
                 [self.view addSubview:newImage];
                 isPaint = !isPaint;
             }
             newImage = [self creatRectangle:newImage withRect:faceRectFit  withcolor:[UIColor blackColor]];
         });
 
 */
        switch (remindCode) {
            case LivenessRemindCodeOK: {
                weakSelf.hasFinished = YES;
                [self warningStatus:CommonStatus warning:@"非常好"];
                if (images[@"image"] != nil && [images[@"image"] count] != 0) {
                    
                    NSArray *imageArr = images[@"image"];
                    for (FaceCropImageInfo * image in imageArr) {
                        NSLog(@"cropImageWithBlack %f %f", image.cropImageWithBlack.size.height, image.cropImageWithBlack.size.width);
                        NSLog(@"originalImage %f %f", image.originalImage.size.height, image.originalImage.size.width);
                    }

                    FaceCropImageInfo * bestImage = imageArr[0];
                    NSLog(@"qualityScore = %f  silentliveScore = %f",bestImage.qualityScore,bestImage.silentliveScore);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        FaceMonitorViewController *vc = FaceMonitorViewController.new;
                        [self showViewController:vc sender:self];
                    });
                    
                    
                    
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.remindAnimationView stopActionAnimating];
                });
                
                [self singleActionSuccess:true];
                
                break;
            }
            case LivenessRemindCodePitchOutofDownRange:
                [self warningStatus:PoseStatus warning:@"请略微抬头" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodePitchOutofUpRange:
                [self warningStatus:PoseStatus warning:@"请略微低头" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeYawOutofRightRange:
                [self warningStatus:PoseStatus warning:@"请略微向右转头" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeYawOutofLeftRange:
                [self warningStatus:PoseStatus warning:@"请略微向左转头" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodePoorIllumination:
                [self warningStatus:CommonStatus warning:@"请使环境光线再亮些" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeNoFaceDetected:
                [self warningStatus:CommonStatus warning:@"把脸移入框内" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeImageBlured:
                [self warningStatus:PoseStatus warning:@"请握稳手机" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeOcclusionLeftEye:
                [self warningStatus:occlusionStatus warning:@"左眼有遮挡" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeOcclusionRightEye:
                [self warningStatus:occlusionStatus warning:@"右眼有遮挡" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeOcclusionNose:
                [self warningStatus:occlusionStatus warning:@"鼻子有遮挡" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeOcclusionMouth:
                [self warningStatus:occlusionStatus warning:@"嘴巴有遮挡" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeOcclusionLeftContour:
                [self warningStatus:occlusionStatus warning:@"左脸颊有遮挡" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeOcclusionRightContour:
                [self warningStatus:occlusionStatus warning:@"右脸颊有遮挡" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeOcclusionChinCoutour:
                [self warningStatus:occlusionStatus warning:@"下颚有遮挡" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeLeftEyeClosed:
                [self warningStatus:occlusionStatus warning:@"左眼未睁开" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeRightEyeClosed:
                [self warningStatus:occlusionStatus warning:@"右眼未睁开" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeTooClose:
                [self warningStatus:CommonStatus warning:@"请将脸部离远一点" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeTooFar:
                [self warningStatus:CommonStatus warning:@"请将脸部靠近一点" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeBeyondPreviewFrame:
                [self warningStatus:CommonStatus warning:@"把脸移入框内" conditionMeet:false];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeLiveEye:
                [self warningStatus:CommonStatus warning:@"眨眨眼" conditionMeet:true];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeLiveMouth:
                [self warningStatus:CommonStatus warning:@"张张嘴" conditionMeet:true];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeLiveYawRight:
                [self warningStatus:CommonStatus warning:@"向右缓慢转头" conditionMeet:true];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeLiveYawLeft:
                [self warningStatus:CommonStatus warning:@"向左缓慢转头" conditionMeet:true];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeLivePitchUp:
                [self warningStatus:CommonStatus warning:@"缓慢抬头" conditionMeet:true];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeLivePitchDown:
                [self warningStatus:CommonStatus warning:@"缓慢低头" conditionMeet:true];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeLiveYaw:
                [self warningStatus:CommonStatus warning:@"左右摇头" conditionMeet:true];
                [self singleActionSuccess:false];
                break;
            case LivenessRemindCodeSingleLivenessFinished:
            {
                [[IDLFaceLivenessManager sharedInstance] livenessProcessHandler:^(float numberOfLiveness, float numberOfSuccess, LivenessActionType currenActionType) {
                    NSLog(@"Finished 非常好 %d %d %d", (int)numberOfLiveness, (int)numberOfSuccess, (int)currenActionType);
                   dispatch_async(dispatch_get_main_queue(), ^{
                       [self.circleProgressView setPercent:(CGFloat)(numberOfSuccess / numberOfLiveness)];
                   });
                }];
                [self warningStatus:CommonStatus warning:@"非常好" conditionMeet:true];
                [self singleActionSuccess:true];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.remindAnimationView stopActionAnimating];
                });
            }
                break;
            case LivenessRemindCodeFaceIdChanged:
            {
                [[IDLFaceLivenessManager sharedInstance] livenessProcessHandler:^(float numberOfLiveness, float numberOfSuccess, LivenessActionType currenActionType) {
                    NSLog(@"face id changed %d %d %d", (int)numberOfLiveness, (int)numberOfSuccess, (int)currenActionType);
                   dispatch_async(dispatch_get_main_queue(), ^{
                       [self.circleProgressView setPercent:0];
                   });
                }];
                [self warningStatus:CommonStatus warning:@"把脸移入框内" conditionMeet:true];
            }
                break;
            case LivenessRemindCodeVerifyInitError:
                [self warningStatus:CommonStatus warning:@"验证失败"];
                break;
//            case LivenessRemindCodeVerifyDecryptError:
//                [self warningStatus:CommonStatus warning:@"验证失败"];
//                break;
//            case LivenessRemindCodeVerifyInfoFormatError:
//                [self warningStatus:CommonStatus warning:@"验证失败"];
//                break;
//            case LivenessRemindCodeVerifyExpired:
//                [self warningStatus:CommonStatus warning:@"验证失败"];
//                break;
//            case LivenessRemindCodeVerifyMissRequiredInfo:
//                [self warningStatus:CommonStatus warning:@"验证失败"];
//                break;
//            case LivenessRemindCodeVerifyInfoCheckError:
//                [self warningStatus:CommonStatus warning:@"验证失败"];
//                break;
//            case LivenessRemindCodeVerifyLocalFileError:
//                [self warningStatus:CommonStatus warning:@"验证失败"];
//                break;
//            case LivenessRemindCodeVerifyRemoteDataError:
//                [self warningStatus:CommonStatus warning:@"验证失败"];
//                break;
            case LivenessRemindCodeTimeout: {
                // 时间超时，重置之前采集数据
                 [[IDLFaceLivenessManager sharedInstance] reset];
                dispatch_async(dispatch_get_main_queue(), ^{
                    // 时间超时，ui进度重置0
                    [self.circleProgressView setPercent:0];
                    [self isTimeOut:YES];
                });
                break;
            }
            case LivenessRemindActionCodeTimeout:{
                [[IDLFaceLivenessManager sharedInstance] livenessProcessHandler:^(float numberOfLiveness, float numberOfSuccess, LivenessActionType currenActionType) {
                    NSLog(@"动作超时 %d %d %d", (int)numberOfLiveness, (int)numberOfSuccess, (int)currenActionType);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.remindAnimationView startActionAnimating:(int)currenActionType];
                    });
                }];
                break;
            }
            case LivenessRemindCodeConditionMeet: {
            }
                break;
            default:
                break;
        }
    }];
}
- (void)singleActionSuccess:(BOOL)success
{
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (success) {
            
        }else {
            
        }
    });
}

- (void)warningStatus:(WarningStatus)status warning:(NSString *)warning conditionMeet:(BOOL)meet{
    [self warningStatus:status warning:warning];
}
- (void)warningStatus:(WarningStatus)status warning:(NSString *)warning
{
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (status == PoseStatus) {
            [weakSelf.remindLabel setHidden:false];
            [weakSelf.remindView setHidden:false];
            [weakSelf.remindDetailLabel setHidden:false];
            weakSelf.remindDetailLabel.text = warning;
            weakSelf.remindLabel.text = @"请保持正脸";
        }else if (status == occlusionStatus) {
            [weakSelf.remindLabel setHidden:false];
            [weakSelf.remindView setHidden:true];
            [weakSelf.remindDetailLabel setHidden:false];
            weakSelf.remindDetailLabel.text = warning;
            weakSelf.remindLabel.text = @"脸部有遮挡";
        }else {
            [weakSelf.remindLabel setHidden:false];
            [weakSelf.remindView setHidden:true];
            [weakSelf.remindDetailLabel setHidden:true];
            weakSelf.remindLabel.text = warning;
        }
    });
}

- (void)captureError {
    NSString *errorStr = @"出现未知错误，请检查相机设置";
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        errorStr = @"相机权限受限,请在设置中启用";
    }
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:errorStr preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action = [UIAlertAction actionWithTitle:@"知道啦" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"知道啦");
        }];
        [alert addAction:action];
        UIViewController* fatherViewController = weakSelf.presentingViewController;
        [weakSelf dismissViewControllerAnimated:YES completion:^{
            [fatherViewController presentViewController:alert animated:YES completion:nil];
        }];
    });
}
@end
